package com.lq.ecoldchain.ui.mine.user.phone

import android.arch.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast

/*
*2019/1/3 0003  17:17
*function by lq
*/
class UpdatePhoneViewModel : BaseViewModel() {
    val phone by lazy { MutableLiveData<String>() }
    val code by lazy { MutableLiveData<String>() }


    fun sendCode() {
        BaseNetCore.ApiServiceImpl.sendUpdatePhoneNewCode(phone.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
            override fun onSuccess(t: String?) {
                toast { "发送成功" }
            }

            override fun onFail(code: String) {
                toast { "发送失败" }
            }
        })
    }

    fun commit() {
        if (code.value.isNullOrEmpty()) {
            toast { "请填写好数据" }
        } else {
            BaseNetCore.ApiServiceImpl.modifyPhoneValidate(code.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
                override fun onSuccess(t: String?) {
                    if (!t.isNullOrEmpty()) {
                        toast { "验证成功" }
                        ARouter.getInstance().build(BaseNavigator.newPhone).withString("token", t).navigation()
                    } else {
                        toast { "验证失败" }
                    }
                }

                override fun onFail(code: String) {}
            })
        }
    }
}